import {
	Controller,
	Post,
	Body,
	UseGuards,
	Request,
	Get,
	Delete,
	Put,
	Param,
	Query,
} from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { CreateProductDto } from './dto/create-product.dto';
import { ProductsService } from './product.service';
import { Product } from './product.entity';
import { RemoveProductDto } from './dto/remove-product.dto';
import { RetrieveProductDto } from './dto/retrieve-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
import { FindProductDto } from './dto/find-product.dto';
import { JwtAuthGuardUser } from 'src/auth/guards/jwt-auth.guard';

@ApiTags('产品')
@Controller('api/products')
export class ProductsController {
	constructor(private readonly productsService: ProductsService) {}

	// 增加
	@UseGuards(JwtAuthGuardUser)
	@Post()
	@ApiOperation({ summary: '增加' })
	async create(@Body() createProductDto: CreateProductDto): Promise<Product> {
		return await this.productsService.create(createProductDto);
	}

	// 删除
	@UseGuards(JwtAuthGuardUser)
	@Delete()
	@ApiOperation({ summary: '删除' })
	async remove(@Body() removeProductDto: RemoveProductDto): Promise<any> {
		return await this.productsService.delete(removeProductDto);
	}

	// 更新
	@UseGuards(JwtAuthGuardUser)
	@Put(':id')
	@ApiOperation({ summary: '更新' })
	async update(
		@Param() params: RetrieveProductDto,
		@Body() updateProductDto: UpdateProductDto,
	): Promise<any> {
		return await this.productsService.update({
			id: params.id,
			updateProductDto,
		});
	}

	// 列表
	// @UseGuards(JwtAuthGuard)
	@Get()
	@ApiOperation({ summary: '列表' })
	async findAll(@Query() query: FindProductDto): Promise<Product> {
		return await this.productsService.findAll(query);
	}

	// 根据 id 查找
	@Get(':id')
	@ApiOperation({ summary: '根据 id 查找' })
	async findOneById(@Param() params: RetrieveProductDto): Promise<any> {
		return await this.productsService.findOneById(params.id);
	}

	// 数量
	@Get('list/count')
	@ApiOperation({ summary: '用户数量' })
	async getCount() {
		return await this.productsService.getCount();
	}
}
